import { createRouter,  createWebHashHistory,createWebHistory } from 'vue-router'
import { usePermissStore } from '../stores/usePermiss';
import {useMenu} from '../stores/Menu'

const router = createRouter({
  history: createWebHashHistory(),
  // history:createWebHistory,
  routes: [
    {
      path:'/',
      redirect:'/dashboard'
    },
    {
      path: '/',
      name: 'home',
      component: () => import('../views/Home.vue'),
      children:[
        {
          path:'/dashboard',
          name:'dashboard',
          meta:{
            title:'系统首页',
            permiss:'1',
          },
          component:()=>import('../views/system/paramInfo.vue')  
        },
        {
          path:'/system/paramInfo',
          name:'paramInfo',
          meta:{
            title:'参数信息',
            permiss:'1',
          },
          component:()=>import('../views/system/paramInfo.vue')  
        },
        {
          path:'/system/config',
          name:'config',
          meta:{
            title:'配置信息',
            permiss:'1',
          },
          component:()=>import('../views/system/config.vue')  
        },
        {
          path:'/system/backup',
          name:'backup',
          meta:{
            title:'数据库备份',
            permiss:'1',
          },
          component:()=>import('../views/system/backup.vue')  
        },
        {
          path:'/user/adminList',
          name:'adminList',
          meta:{
            title:'管理员管理',
            permiss:'2',
          },
          component:()=>import('../views/user/adminList.vue')  
        },
        {
            path:'/user/stuList',
            name:'stuList',
            meta:{
              title:'用户管理',
              permiss:'2'
            },
            component:()=>import('../views/user/stuList.vue')
        },
        {
          path:'/user/teacherList',
          name:'teacherList',
          meta:{
            title:'教师管理',
            permiss:'2'
          },
          component:()=>import('../views/user/teacherList.vue')
          },
          {
            path:'/user/advisorList',
            name:'advisorList',
            meta:{
              title:'咨询师管理',
              permiss:'2'
            },
            component:()=>import('../views/user/advisorList.vue')
        },

        {
          path:'/privilege/userGroup',
          name:'userGroup',
          meta:{
            title:'用户组管理',
            permiss:'16',
          },
          component:()=>import('../views/privilege/userGroup.vue')  
        },
        {
          path:'/privilege/roleList',
          name:'roleList',
          meta:{
            title:'角色管理',
            permiss:'19',
          },
          component:()=>import('../views/privilege/roleList.vue')  
        },
        {
          path:'/privilege/rolePriv',
          name:'rolePriv',
          meta:{
            title:'角色权限',
            permiss:'35',
          },
          component:()=>import('../views/privilege/rolePriv.vue')  
        },
        {
          path:'/privilege/menuList',
          name:'menuList',
          meta:{
            title:'菜单管理',
            permiss:'38',
          },
          component:()=>import('../views/privilege/menuList.vue')  
        },
        {
          path:'/privilege/menuAction',
          name:'menuAction',
          meta:{
            title:'菜单行为',
            permiss:'37',
          },
          component:()=>import('../views/privilege/menuAction.vue')  
        },
        {
          path:'/privilege/actionList',
          name:'actionList',
          meta:{
            title:'行为管理',
            permiss:'39',
          },
          component:()=>import('../views/privilege/actionList.vue')  
        },
        {
          path:'/clazz/major',
          name:'major',
          meta:{
            title:'专业方向',
            permiss:'50',
          },
          component:()=>import('../views/clazz/major.vue')  
        },
        {
          path:'/clazz/clazzList',
          name:'clazzList',
          meta:{
            title:'班级列表',
            permiss:'51',
          },
          component:()=>import('../views/clazz/clazzList.vue')  
        },
        {
          path:'/clazz/question',
          name:'question',
          meta:{
            title:'班级问题',
            permiss:'52',
          },
          component:()=>import('../views/clazz/question.vue')  
        },
        {
          path:'/clazz/homework',
          name:'homework',
          meta:{
            title:'布置作业',
            permiss:'53',
          },
          component:()=>import('../views/clazz/homework.vue')  
        },
        {
          path:'/clazz/studyProcess',
          name:'studyProcess',
          meta:{
            title:'学习进度',
            permiss:'54',
          },
          component:()=>import('../views/clazz/studyProcess.vue')  
        },
        
        {
          path:'/dataTable',
          name:'dataTable',
          meta:{
            title:'数据统计',
            permiss:'54',
          },
          component:()=>import('../views/analysis/TableData.vue')  
        },
        
        {
          path:'/notice/noticeList',
          name:'noticeList',
          meta:{
            title:'公告管理',
            permiss:'67',
          },
          component:()=>import('../views/notice/noticeList.vue')  
        },
        {
          path:'/notice/newsList',
          name:'newsList',
          meta:{
            title:'新闻资讯',
            permiss:'68',
          },
          component:()=>import('../views/notice/newsList.vue')  
        },
        
        {
          path:'/log/list',
          name:'logList',
          meta:{
            title:'日志列表',
            permiss:'34',
          },
          component:()=>import('../views/log/list.vue')  
        },
        {
          path:'/organization/branchList',
          name:'branchList',
          meta:{
            title:'分支机构',
            permiss:'65',
          },
          component:()=>import('../views/organization/branchList.vue')  
        },
        {
          path:'/organization/orgList',
          name:'orgList',
          meta:{
            title:'机构列表',
            permiss:'66',
          },
          component:()=>import('../views/organization/orgList.vue')  
        },
        {
          path:'/resources/resourceType',
          name:'resourceType',
          meta:{
            title:'资源管理',
            permiss:'40',
          },
          component:()=>import('../views/resources/resourceType.vue')  
        },
        {
          path:'/resources/resourceList',
          name:'resourceList',
          meta:{
            title:'文档管理',
            permiss:'45',
          },
          component:()=>import('../views/resources/resourceList.vue')  
        },
        {
          path:'/resources/videoMgmt',
          name:'videoMgmt',
          meta:{
            title:'视频管理',
            permiss:'46',
          },
          component:()=>import('../views/resources/videoMgmt.vue')  
        },
        {
          path:'/training/schemaList',
          name:'schemaList',
          meta:{
            title:'方案列表',
            permiss:'47',
          },
          component:()=>import('../views/training/schemaList.vue')  
        },
        {
          path:'/training/courseList',
          name:'courseList',
          meta:{
            title:'课程列表',
            permiss:'48',
          },
          component:()=>import('../views/training/courseList.vue')  
        },
        {
          path:'/training/courseSetting',
          name:'courseSetting',
          meta:{
            title:'课程列表',
            permiss:'48',
          },
          component:()=>import('../views/training/courseSetting.vue')  
        },
        {
          path:'/training/courseCategory',
          name:'courseCategory',
          meta:{
            title:'课程分类',
            permiss:'49',
          },
          component:()=>import('../views/training/courseCategory.vue')  
        },
        {
          path:'/evaluation/pollItem',
          name:'courseCategoryt',
          meta:{
            title:'考评选项',
            permiss:'60',
          },
          component:()=>import('../views/evaluation/pollItem.vue')  
        },
       
        {
          path:'/evaluation/pollTemplate',
          name:'pollTemplate',
          meta:{
            title:'考评模版',
            permiss:'61',
          },
          component:()=>import('../views/evaluation/pollTemplate.vue')  
        },
        {
          path:'/evaluation/teacherPoll',
          name:'teacherPoll',
          meta:{
            title:'教师考评',
            permiss:'62',
          },
          component:()=>import('../views/evaluation/teacherPoll.vue')  
        },
        {
          path:'/teaching/teaching',
          name:'teaching',
          meta:{
            title:'课堂教学',
            permiss:'55',
          },
          component:()=>import('../views/teaching/teaching.vue')  
        },
        {
          path:'/enterClass',
          name:'enterClass',
          meta:{
            title:'课堂教学',
            permiss:'55',
          },
          component:()=>import('../views/teaching/enterClass.vue')  
        },
       
        {
          path:'/advisor/advice',
          name:'advice',
          meta:{
            title:'建议咨询',
            permiss:'69',
          },
          component:()=>import('../views/advisor/advice.vue')  
        },
        {
          path:'/potentialClient/clientList',
          name:'clientList',
          meta:{
            title:'客户列表',
            permiss:'70',
          },
          component:()=>import('../views/potentialClient/clientList.vue')  
        },
        {
          path:'/potentialClient/dataReport',
          name:'dataReport',
          meta:{
            title:'数据报表',
            permiss:'71',
          },
          component:()=>import('../views/potentialClient/dataReport.vue')  
        },
        {
          path:'/exam/titleList',
          name:'titleList',
          meta:{
            title:'题库管理',
            permiss:'56',
          },
          component:()=>import('../views/exam/titleList.vue')  
        },
        {
          path:'/exam/testItemList',
          name:'testItemList',
          meta:{
            title:'试题管理',
            permiss:'57',
          },
          component:()=>import('../views/exam/testItemList.vue')  
        },
        {
          path:'/exam/paperManagementList',
          name:'paperManagementList',
          meta:{
            title:'试卷管理',
            permiss:'58',
          },
          component:()=>import('../views/exam/paperManagementList.vue')  
        },
        {
          path:'/exam/examManagementList',
          name:'examManagementList',
          meta:{
            title:'考试汇总',
            permiss:'59',
          },
          component:()=>import('../views/exam/examManagementList.vue')  
        },
        {
          path:'/analysis/clazzCount',
          name:'/analysis/clazzCount',
          meta:{
            title:'班级统计',
            permiss:'63',
          },
          component:()=>import('../views/analysis/clazzCount.vue')  
        },
        {
          path:'/analysis/watchVideoCount',
          name:'watchVideoCount',
          meta:{
            title:'学员观看记录',
            permiss:'64',
          },
          component:()=>import('../views/analysis/watchVideoCount.vue')  
        },
       
        
      ]
    },
    {
      path: '/login',
      name: 'login',
      meta: {
          title: '登录',
      },
      component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue'),
  },
  {
      path: '/403',
      name: '403',
      meta: {
          title: '没有权限',
      },
      component: () => import(/* webpackChunkName: "403" */ '../views/403.vue'),
  },
  ]
})
router.beforeEach((to, from, next) => {
  document.title = `乐码在线智慧平台 - ${to.meta.title}`;
  const role = localStorage.getItem('ms_username');
  let token  = localStorage.getItem('token')
  const permiss = usePermissStore();
  console.log(to,from)
  console.log(to.meta.permiss)
  console.log("permiss>>>>",permiss.key)
  console.log(permiss.key.includes(Number(to.meta.permiss)))
  if (!role && to.path !== '/login') {
      next('/login');
  } else if (to.meta.permiss && !permiss.key.includes(Number(to.meta.permiss))) {
      // 如果没有权限，则进入403
      next('/403');
  // }else if(token==null||token===''){
  //   next('/login')
  } else {
      next();
  }
});


export default router
